<template>
  <t-modal
    :title="title"
    :width="700"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok');
      }
    "
    @cancel="
      () => {
        $emit('cancel');
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form">
        <a-form-item class="hiddenItem">
          <!-- uuid隐藏 -->
          <a-input v-decorator="['uuid']" disabled type="hidden" />
        </a-form-item>
        <a-row>
          <a-col :span="12">
            <a-form-item
              label="上级公司"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <depart-popup
                placeholder="请选择上级公司"
                url="/mdata/organization/getTreeSysOrganization"
                v-decorator="[
                  'pid',
                  { rules: [{ required: true, message: '请选择上级公司' }] },
                ]"
              ></depart-popup>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              label="部门名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                placeholder="请输入部门名称"
                v-decorator="[
                  'orgName',
                  { rules: [{ required: true, min: 1, message: '必填' }] },
                ]"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              label="部门分类"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <t-dict-select-tag
                type="select"
                dictCode="departmentType"
                placeholder="请选择所属行业"
                v-decorator="[
                  'departmentType',
                  { rules: [{ required: true, message: '请选择部门分类' }] },
                ]"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12" v-show="false">
            <a-form-item
              label="部门编码"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                placeholder="请输入部门编码"
                v-decorator="[
                  'orgCode',
                  { rules: [{ required: false, min: 1, message: '必填' }] },
                ]"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              label="排序"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input-number
                style="width: 100%;"
                :min="1"
                :max="100"
                v-decorator="['orgSort']"
                placeholder="请输入排序"
              />
            </a-form-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-item
              label="部门负责人"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <personnel-popup
                placeholder="请选择部门负责人"
                v-decorator="[
                  'orgPerson',
                  { rules: [{ required: true, message: '请选择部门负责人' }] },
                ]"
              ></personnel-popup>
            </a-form-item>
          </a-col> -->

          <a-col :span="24">
            <a-form-item
              label="备注"
              :labelCol="{ xs: { span: 24 }, sm: { span: 3 } }"
              :wrapperCol="{ xs: { span: 24 }, sm: { span: 21 } }"
            >
              <a-textarea
                placeholder="请输入备注"
                :auto-size="{ minRows: 3, maxRows: 5 }"
                v-decorator="['orgBriefIntroduction']"
              />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </t-modal>
</template>

<script>
import pick from "lodash.pick";
import DepartPopup from "@/components/TopVUI/custom/DepartPopup";
import PersonnelPopup from "@/components/TopVUI/custom/PersonnelPopup";
import { TDictSelectTag } from "@/components";
// 表单字段
const fields = [
  "uuid",
  "pid",
  "orgName",
  "orgCode",
  "departmentType",
  "orgPerson",
  "orgSort",
  "orgBriefIntroduction",
];

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  components: {
    DepartPopup,
    PersonnelPopup,
    TDictSelectTag,
  },
  data() {
    return {
      labelCol: {
        xs: {
          span: 24,
        },
        sm: {
          span: 6,
        },
      },
      wrapperCol: {
        xs: {
          span: 24,
        },
        sm: {
          span: 18,
        },
      },
      form: this.$form.createForm(this),
      title: "",
    };
  },
  methods: {
    add(m, data) {},
    edit(m, data) {},
  },
  mounted() {
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v));
    // 当 model 发生改变时，为表单设置值
    this.$watch("model", () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields));
    });
  },
};
</script>
